/*****************************************************************************
 ** Copyright (c) 2010 Ushahidi Inc
 ** All rights reserved
 ** Contact: team@ushahidi.com
 ** Website: http://www.ushahidi.com
 **
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser
 ** General Public License version 3 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file. Please review the following information to
 ** ensure the GNU Lesser General Public License version 3 requirements
 ** will be met: http://www.gnu.org/licenses/lgpl.html.
 **
 **
 ** If you have questions regarding the use of this file, please contact
 ** Ushahidi developers at team@ushahidi.com.
 **
 *****************************************************************************/

#import "IncidentTableCell.h"
#import "UIColor+Extension.h"
#import "Device.h"
#import "Settings.h"

#define SEGURIDAD @"seguridad"

@implementation IncidentTableCell

typedef enum {
	VerifiedNo,
	VerifiedYes
} Verified;

@synthesize titleLabel, locationLabel, categoryLabel, dateLabel, verifiedLabel, imageView, activityIndicator;

- (void)dealloc {
	[titleLabel release];
	[locationLabel release];
	[categoryLabel release];
	[dateLabel release];
	[imageView release];
	[verifiedLabel release];
	[activityIndicator release];
    [super dealloc];
}

- (void) setTitle:(NSString *)title {
	self.titleLabel.text = title;
}

- (NSString *) title {
	return self.titleLabel.text;
}

- (void) setLocation:(NSString *)location {
	if (location != nil && [location length] > 0) {
		self.locationLabel.text = location;
	}
	else {
		self.locationLabel.text = NSLocalizedString(@"No Location Specified", nil);
	}
}

- (NSString *) location {
	return self.titleLabel.text;
}

- (void) setCategory:(NSString *)category {
	if (category != nil && [category length] > 0) {
		self.categoryLabel.text = category;
	}
	else {
		self.categoryLabel.text = NSLocalizedString(@"No Category Specified", nil);
	}
}

- (NSString *) category {
	return self.categoryLabel.text;
}

- (void) setDate:(NSString *)date {
	if (date != nil && [date length] > 0) {
		self.dateLabel.text = date;
	}
	else {
		self.dateLabel.text = NSLocalizedString(@"No Date Specified", nil);
	}
}

- (NSString *) date {
	return self.dateLabel.text;
}

- (bool) checkCategory:(NSArray*)group forImage:(NSString*)image {
	for (NSString* item in group) {
		NSLog(@"checkCategory %@ ? %@ : %@", [self category], item, image);
		if ([[self category] rangeOfString:item options:NSCaseInsensitiveSearch].location != NSNotFound) {
			self.imageView.image = [UIImage imageNamed:image];
			return false;
		}
	}
	return true;
}

- (void) setImageFromCategory {

	static NSArray *comunidad=nil, *vialidadyTransito, *seguridad, *emergenciaMedica, *serviciosPublicos;
	
	
	if (!comunidad) {
		comunidad = [[NSArray arrayWithObjects:
					 @"Avisos",
					 @"Evento Publico",
					 @"Parques Descuidados",
					 nil] retain];
		
		emergenciaMedica = [[NSArray arrayWithObjects:
							@"Emergencia",
							nil] retain];
		
		vialidadyTransito = [[NSArray arrayWithObjects:
							 @"Accidente",
							 @"Bache",
							 @"via dañada",
							 @"Obras",
							 @"via cerrada",
							 @"Semaforo descompuesto",
							 @"Auto Abandonado",
							 nil] retain];
		
		serviciosPublicos = [[NSArray arrayWithObjects:
							 @"Alumbrado publico",
							 @"Fuga de Agua",
							 @"Falta de Electricidad",
							 @"Parque",
							 nil] retain];
		
		seguridad = [[NSArray arrayWithObjects:
					 @"Situacion de Riesgo",
					 @"Robo",
					 @"Secuestro",
					 @"Extorsion",
					 @"Sospechoso",
					 @"Incendio",
					 nil] retain];
	}

	if ([self checkCategory:comunidad forImage:@"comunidad.png"] &&
		[self checkCategory:emergenciaMedica forImage:@"emergenciasMedicas.png"] &&
		[self checkCategory:vialidadyTransito forImage:@"vialidadyTransito.png"] &&
		[self checkCategory:serviciosPublicos forImage:@"serviciosPublicos.png"] &&
		[self checkCategory:seguridad forImage:@"seguridad.png"])
		self.imageView.image = [UIImage imageNamed:@"placeholder.png"];
}

- (void) setImage:(UIImage *)image {

	NSLog(@"category, title = %@, %@", [self category], [self title]);
	if (image != nil)
		self.imageView.image = image;
	else
		[self setImageFromCategory];
}

- (UIImage *) image {
	return self.imageView.image;
}

- (void) setSelectedColor:(UIColor *)color {
	UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
	[selectedBackgroundView setBackgroundColor:color];
	[self setSelectedBackgroundView:selectedBackgroundView];
	[selectedBackgroundView release];	
}

- (UIColor *) selectedColor {
	return self.selectedBackgroundView.backgroundColor;
}

- (void) setVerified:(BOOL) verified {
	if (verified) {
//		self.verifiedLabel.text = NSLocalizedString(@"Verified", nil);
		self.verifiedLabel.text = NSLocalizedString(@"", nil);
		self.verifiedLabel.textColor = [[Settings sharedSettings] verifiedTextColor];
		self.verifiedLabel.tag = VerifiedYes;
	}
	else {
//		self.verifiedLabel.text = NSLocalizedString(@"Unverified", nil);
		self.verifiedLabel.text = NSLocalizedString(@"", nil);
		self.verifiedLabel.textColor = [[Settings sharedSettings] unverifiedTextColor];
		self.verifiedLabel.tag = VerifiedNo;
	}
}

- (BOOL) verified {
	return self.verifiedLabel.tag == VerifiedYes;
}

+ (CGFloat) getCellHeight {
	return [Device isIPad] ? 110 : 90;
}

- (BOOL) uploading {
	return [self.activityIndicator isAnimating];
}

- (void) setUploading:(BOOL)isUploading {
	if (isUploading) {
		[self.activityIndicator startAnimating];
	}
	else {
		[self.activityIndicator stopAnimating];
	}
}

@end
